package com.siwei.infoextract.wrapper.featureextract;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.siwei.infoextract.wrapper.parser.DomNode;
import com.siwei.infoextract.wrapper.parser.DomNodeIteratorCallback;
import com.siwei.infoextract.wrapper.parser.DomTreeTraversal;

public class TrainedFeaturExtractor {
	private static InnerTextFeatureExtractor innerTextExtractor = new InnerTextFeatureExtractor();

	private static Pattern titlePattern = Pattern.compile("\\$title.*?\\$/title", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
	private static Pattern addressPattern = Pattern.compile("\\$address.*?\\$/address", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
	private static Pattern phonePattern  = Pattern.compile("\\$phone.*?\\$/phone", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);

	private List<AddressFeatureVector> addressFeatureVectors = new ArrayList<AddressFeatureVector>();    
	private List<PhoneFeatureVector> phoneFeatureVectors = new ArrayList<PhoneFeatureVector>();
	private List<TitleFeatureVector> titleFeatureVectors = new ArrayList<TitleFeatureVector>();

	private DomTreeTraversal traversal;
	private DomNode domNode;

	public TrainedFeaturExtractor(DomNode domNode) {
		this.domNode = domNode;
		traversal = new DomTreeTraversal(domNode);
		traversal.addCallback(new DomNodeIteratorCallback() {
			public void call(DomNode domNode) {
				String innerText = (String)innerTextExtractor.extract(domNode);

				Matcher matcher = titlePattern.matcher(innerText);
				if (matcher.find()) {
					TitleFeatureVector titleFeatureVector = new TitleFeatureVector(); 
					titleFeatureVector.setInnerText(innerText);
					titleFeatureVector.setTagName(domNode.getName());
					titleFeatureVectors.add(titleFeatureVector);

					return;
				}

				matcher = addressPattern.matcher(innerText);
				if (matcher.find()) {
					AddressFeatureVector addressFeatureVector = new AddressFeatureVector();
					addressFeatureVector.setInnerText(innerText);
					addressFeatureVector.setTagName(domNode.getName());

					DomNode prevSibling = domNode.getPrevSibling();
					if (prevSibling != null) {
						addressFeatureVector.setPrevSiblingInnerText((String)innerTextExtractor.extract(prevSibling));
					}
					addressFeatureVectors.add(addressFeatureVector);

					return;
				}
				
				matcher = phonePattern.matcher(innerText);
				if (matcher.find()) {
					PhoneFeatureVector phoneFeatureVector = new PhoneFeatureVector();
					phoneFeatureVector.setInnerText(innerText);
					phoneFeatureVector.setTagName(domNode.getName());

					DomNode prevSibling = domNode.getPrevSibling();
					if (prevSibling != null) {
						phoneFeatureVector.setPrevSiblingInnerText((String)innerTextExtractor.extract(prevSibling));
					}
					phoneFeatureVectors.add(phoneFeatureVector);
					return;
				}
			}
		});
	}

	public void extract() {
		traversal.traverse();
	}

	public List<AddressFeatureVector> getAddressFeatureVectors() {
		return addressFeatureVectors;
	}
	
	public List<PhoneFeatureVector> getPhoneFeatureVectors() {
		return phoneFeatureVectors;
	}

	public List<TitleFeatureVector> getTitleFeatureVectors() {
		return titleFeatureVectors;
	}
	
}

